package com.tdam2014.grupo02;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.tdam2014.grupo02.DataBase.DB;
import com.tdam2014.grupo02.DataBase.DBHelper;

public class Folder {

private String folderId;
private String folderName;
private String folderDescription;
private String folderPhoto;
private String folderFlickrId;
private ArrayList<Picture> pictures;
private static int dbVersion = 4;


	public Folder (String name, String description, String photo, String flickrId)
	{
		this.folderDescription = description;
		this.folderName = name;
		this.folderPhoto = photo;
		this.setPictures(null);
		this.folderFlickrId = flickrId;
	}

	public Folder (JSONObject jsonObj)
	{
		this.folderDescription = jsonObj.optString("title");
		this.folderName = jsonObj.optString("primary");
		//	this.folderPhoto = jsonObj.optString("folderPhoto");
		this.folderFlickrId = jsonObj.optString("id");
		//	this.setPictures(null);
	}

	public Folder(Cursor cursor) 
	{
		cursor.moveToFirst();
		this.folderDescription = cursor.getString(cursor.getColumnIndex("folderDescription"));
		this.folderName = cursor.getString(cursor.getColumnIndex("folderName"));
		this.folderPhoto = cursor.getString(cursor.getColumnIndex("folderPhoto"));
		this.folderFlickrId = cursor.getString(cursor.getColumnIndex("folderflickrId"));
		
	// TODO Auto-generated constructor stub
	}

	public void setFolderDescription (String value)
	{
		this.folderDescription = value;
	}
	public String getFolderDescription ()
	{
		return this.folderDescription;
	}
	
	public void setFolderName (String value)
	{
		this.folderName = value;
	}
	public String getFolderName ()
	{
		return this.folderName;
	}
	
	public void setFolderPhoto (String value)
	{
		this.folderPhoto = value;
	}
	public String getFolderPhoto ()
	{
		return this.folderPhoto;
	}
	
	public static ArrayList<Folder> generateFolder() 
	{
		ArrayList<Folder> list = new ArrayList<Folder>();
		for (int i = 0; i < 25; i++) {
			list.add(new Folder("Carpeta " + i, "Carpeta " + i, "Carpeta " + i, "" + i));
		}
		return list;
	}
	public static void generateFolders(Context context) 
	{
		DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
		for (int i = 0; i < 25; i++) 
		{
			Folder f = new Folder("Carpeta " + i, "Carpeta " + i, "Carpeta " + i, "" + i);
			dbHelper.newFolder(f, context);
			//DBHelper.newFolder(f, context);
			//DBHelper (f, context);
		}
	}
	public ArrayList<Picture> getPictures() {
		return pictures;
	}
	public void setPictures(ArrayList<Picture> pictures) {
		this.pictures = pictures;
	}
	
    public static ArrayList<Folder> getFolders(Context context)
    {
        DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    		
   	    return dbHelper.getFolders(context);
    }
    public static void updateFolder (Context context) throws ClientProtocolException, JSONException, IOException
    {
    	DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    	dbHelper.updateFolder(FlickrConnect.getFolders(), context);
    }

	public String getFolderId() {
		return folderId;
	}

	public void setFolderId(String folderId) {
		this.folderId = folderId;
	}

	public String getFolderFlickrId() {
		return folderFlickrId;
	}

	public void setFolderFlickrId(String folderFlickrId) {
		this.folderFlickrId = folderFlickrId;
	}
	public static Folder getFolder(Context context, String folderId) 
	{
		DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
		return dbHelper.getFolder(context, Integer.parseInt(folderId));
	}
	public Boolean compareFolders(ArrayList<Folder> lista1, ArrayList<Folder> lista2)
	{
		Boolean iguales = true;
		for (int i=0; i < lista1.size() && iguales == true; i++)
		{
			if (lista1.get(i) !=  lista2.get(i))
			{
				iguales = false;
			}
		}
		return iguales;
		
	}

}
